#require 'mini_magick'
#require 'carrierwave'
require "open-uri"
require 'json'
class Spider
  def self.spider_bt(movie)
    #return movie
    url = "http://www.bttiantang.com"
    param = "/s.php?q=#{movie.imdb}&sitesearch=www.bttiantang.com&domains=bttiantang.com&hl=zh-CN&ie=UTF-8&oe=UTF-8"
    search_url = url + param
    doc = nil
    begin
      doc = Nokogiri::HTML(open(search_url, 'UserAgent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)').read)
    rescue => e
      Rails.logger.warn { "[ERROR] fetch douban cover page failed : #{e} url: #{search_url}" }
      return doc
    end
    result_info = doc.css('div.title')
    if result_info.blank?
      return nil
    end
    detail_url = []
    result_info.each do |info|
      detail =  info.css('p.tt > a').attr('href').to_s
      detail_url << url + detail
    end
    return nil if detail_url.blank?
    #bt_arys = []
    detail_url.each do |bt|
      doc_bt_list = nil
      begin
        doc_bt_list = Nokogiri::HTML(open(bt, 'UserAgent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)').read)
      rescue => e
        Rails.logger.warn { "[ERROR] fetch douban cover page failed : #{e} url: #{bt}" }
        next
      end
      bt_list = doc_bt_list.css('div.tinfo')
      next if bt_list.blank?
      bt_list.each do |list|
        down_url = list.css('a').attr('href').to_s.split('&')
        qu = Attach.has_quality_by_title(list.css('a').text)
        #bt_params = {}
        #bt_params['qu'] = qu
        down_params = {}
        down_url.each do |down|
          down_ary = down.split('=')
          id = down_ary[1] if down_ary[0] == 'id'
          uhash = down_ary[1] if down_ary[0] == 'uhash'
          unless id.blank?
            down_params['id'] = id
            down_params['action'] = 'download'
          end
          unless uhash.blank?
            down_params['uhash'] = uhash
          end
        end
        begin
          uri = URI.parse("http://www.bttiantang.com/download.php")
          res = Net::HTTP.post_form(uri, down_params)
          body  = res.body
        rescue => e
          body = nil
        end
        next if body.nil? || body.blank?
        file_name = Array.new(16){rand(1024).to_s(36)}.join+'.torrent'
        File.open("#{Rails.root}/public/uploads/bt/#{file_name}", 'w') { |file| file.write(body.force_encoding('UTF-8')) }
        #bt_params['url']= file_name
        torrent_info = Attach.read_torrent("#{Rails.root}/public/uploads/bt/#{file_name}")
        File.delete("#{Rails.root}/public/uploads/bt/#{file_name}")
        next if torrent_info.blank? || torrent_info.nil?
        #bt_params['torrent'] = read_torrent
        #bt_arys << bt_params
        fname,flength,magnet_url = torrent_info

        next if flength < 1073741824
        fname = file_name if fname.blank?
        #field :name
        #field :format, :default => "torrent"
        # 屏幕高度，480, 481 ,720,1080,3000
        #field :quality, :type => Integer, :default => 0
        #field :url
        #field :file_size, :type => Integer
        #field :movie_id ,:type => Integer
        movie.attaches.create(:name => fname,:quality => qu,:url=>magnet_url,:file_size=>flength)
      end
    end
    movie
  end





  #获取豆瓣详情页面的信息
  def self.spider_douban_html(douban_id)



    #查询这个资源是否存在，如果存在则只需要更新一些其他的字段，比如评分和评分人数

    this_movie = nil
    begin
      this_movie = Movie.find_by(source_id:douban_id,source:'douban')
    rescue => e
      Rails.logger.warn { "[ERROR] fetch not found source_id=#{douban_id}=====source:'douban'" }
    end


    #通过api开放接口获取豆瓣的详细数据
    douban_api = self.read_douban_api(douban_id)
    api_img = douban_api["images"]["large"]
    #如果已经存在则更新数据
    unless this_movie.nil?
      average = douban_api["rating"]["average"].to_f || 0
      ratings = douban_api["ratings_count"].to_i || 0
      this_movie.update(:average => average,:ratings => ratings)
      movie_photo = this_movie.photo_url
      if movie_photo.include?("photo")
        unless api_img.blank?
          this_movie.update(:photo_remote_url => api_img)
        end
      end
      return this_movie
    end

    #获取html页面的数据
    doc = nil
    url = "http://movie.douban.com/subject/#{douban_id}?apikey=#{Setting.douban_token}"
    begin
      doc = Nokogiri::HTML(open(url, 'UserAgent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)').read)
    rescue => e
      Rails.logger.warn { "[ERROR] fetch douban cover page failed : #{e} url: #{url}" }
      return doc
    end
    doc.css('div#info').each do |info|
      info = info.to_s.gsub("<br/>","<br>\r\n")
      info = info.to_s.gsub("<br>","<br>\r\n")
      regex=/<span class="pl">([\S\s]+?):<\/span>([\S\s]+?)<br>/i
      info.scan(regex).each do |key,val|
        case key
        when "语言"
          douban_api["languages"] = val.gsub(" ","").split("/")
        when "首播"
          douban_api["release_date"] = val.scan(/(\d{1,4}-\d{1,2}-\d{1,2})/i).flatten[0] || nil
        when "集数"
          douban_api["episodes"] = val.scan(/(\d{1,4})/i).flatten[0].to_i || 1
        when "单集片长"
          douban_api["minutes"] = val.scan(/(\d{1,4})/i).flatten[0].to_i || nil
        when '片长'
          douban_api["minutes"] = val.scan(/(\d{1,4})/i).flatten[0].to_i || nil
        when "IMDb链接"
          douban_api["imdb"] = val.scan(/<a href=\"http:\/\/www.imdb.com\/title\/([a-z0-9]*)\"/i).flatten[0] || nil

          #如果没有找到IMDb就视为这个资源无效

        when "上映日期"
          douban_api["release_date"] = val.scan(/(\d{1,4}-\d{1,2}-\d{1,2})/i).flatten[0] || nil
        when "季数"
          #puts val
          selected = val.scan(/selected>(\d{1,3})<\/option>/i).flatten[0].to_i
          if selected < 1
            selected = nil
          end

          html_season = val.scan(/(\d{1,3})/i).flatten[0].to_i

          if html_season < 1
            html_season = nil
          end

          douban_api["season"] = selected || html_season || 1
        else
        end
      end
    end
    #return douban_api
    return nil if douban_api["imdb"].nil?
    data = {}
    other_data = {}
    #影片名称
    data[:title] = douban_api["title"].gsub(/○|、|｜|＼|／|＂|＇|，|．|！/,'')
    data[:summary] = douban_api["summary"].gsub(/\n|\r|\r\n|\n\r|©豆瓣/,'') || data[:title]
    data[:year] = douban_api["year"].to_i
    data[:release_date] = douban_api["release_date"] || data[:year].to_s+"-01-01"
    data[:average] = douban_api["rating"]["average"].to_f || 0
    data[:stars] = douban_api["rating"]["stars"].to_i || 0
    data[:ratings] = douban_api["ratings_count"].to_i || 0
    data[:type] = douban_api["subtype"]
    data[:episodes] = douban_api["episodes"] || 1
    data[:minutes] = douban_api["minutes"] || 0

    data[:categories] = douban_api["genres"]
    data[:countries] = douban_api["countries"]
    data[:languages] = douban_api["languages"]
    #data[:categories] = douban_api["genres"]
    #data[:categories] = douban_api["genres"]
    actors = []
    douban_api["casts"].each do |actor|
      actors << actor["name"]
    end
    data[:actors] = actors

    directors = []
    douban_api["directors"].each do |director|
      directors << director["name"]
    end
    data[:directors] = directors
    data[:aliases] = douban_api["aka"]
    data[:season] = douban_api["season"] || 1
    data[:source] = 'douban'
    data[:imdb] = douban_api["imdb"]
    data[:source_id] = douban_id
    data[:title_en] = self.get_title_en(douban_api["imdb"])
    photo = self.douban_photo(douban_id)
    photo = api_img if photo.nil?
    data[:photo_remote_url] = photo

    #p data
    @movie = Movie.create(data)
    #@moive_update = Movie.other_update(@movie,other_data)
    return @movie
  end


  #通过接口去获取影片的英文名称，便于获取资源
  def self.get_title_en(imdb)
    url = "http://www.omdbapi.com/?i=#{imdb}"
    result = nil
    begin
      result = open(url).read
    rescue => e
      Rails.logger.warn { "Spider.omdbapi_api : #{e} url: #{url}" }
      result = "{}"
    end
    @re = JSON.parse(result)
    unless @re["Title"].nil?
      return @re["Title"]
    else
      return nil
    end

  end

  # 获取高清的电影封面
  def self.douban_photo(douban_id)
    url = "http://movie.douban.com/subject/#{douban_id}/photos?type=R&start=0&sortby=vote&size=a&subtype=o&apikey=#{Setting.douban_token}"
    image_url = nil
    begin
      doc = Nokogiri::HTML(open(url, 'UserAgent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)').read)
    rescue => e
      Rails.logger.warn { "[ERROR] fetch douban cover page failed : #{e} url: #{url}"}
      return image_url
    end

    lis = doc.css(".poster-col4 li")
    lis.each do |li|
      prop_doc   = li.css(".prop")
      label_doc  = li.css(".name")
      label_text = label_doc.blank? ? "" : label_doc.text().strip

      next if prop_doc.blank?
      prop = prop_doc.text().strip
      # 跳过没有 尺寸的
      next if prop.blank?

      # 判断比例, 跳过宽比高大的
      xy = prop.split("x")
      next if xy[0].to_i >= xy[1].to_i
      next if xy[0].to_i < 500
      next if label_text.match(/日本|韩国/)

      # 取得图片
      image_doc = li.css(".cover a img")
      if !image_doc.blank?
        image_url = image_doc.attr('src').to_s.gsub("/thumb","/photo")
        return image_url
      end
    end
    image_url
  end

  def self.download_file(url)
    img = nil
    begin
      img = MiniMagick::Image.read(open(url).read)
    rescue => e
      Rails.logger.error { "Movie.download_file ERROR: #{url} #{e}" }
    end
    return img
  end




  def self.read_douban_api(douban_id)
    # 限制每分钟 38 次
    return nil if !self.allow?
    self.hint
    api_url = "http://api.douban.com/v2/movie/subject/#{douban_id}?apikey=#{Setting.douban_token}"
    begin
      result = open(api_url).read
    rescue => e
      Rails.logger.warn { "Spider.read_douban_api : #{e} url: #{api_url}" }
      result = "{}"
    end
    @re = JSON.parse(result)
    unless @re["id"].nil?
      return @re
    else
      return nil
    end
  end


  def self.read_douban_api_by_imdb(imdb)
    # 限制每分钟 38 次
    return false if !self.allow?
    self.hint
    api_url = "http://api.douban.com/v2/movie/imdb/#{imdb}?apikey=#{Setting.douban_token}"
    begin
      result = open(api_url).read
    rescue => e
      Rails.logger.warn { "Spider.read_douban_api_by_imdb : #{e} url: #{api_url}" }
      result = "{}"
    end
    imdb_result = JSON.parse(result)
    #imdbresult["idasdasd"]
    unless imdb_result["id"].nil?
      self.read_douban_api(imdb_result["id"].gsub("http://api.douban.com/movie/","").to_i)
    else
      nil
    end
  end


  def self.cache_key
    "models/douban/request_count_#{Time.now.strftime("%Y%m%d%H%M")}"
  end

  def self.hint
    Rails.cache.increment(self.cache_key, 1)
  end

  def self.count
    Rails.cache.read(self.cache_key).to_i || 0
  end

  def self.allow?
    self.count < 40
  end
end
